home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / msysjour / vol06 / 01 / wintro4 / tick.c < prev   
C/C++ Source or Header  |  1990-12-31  |  6KB  |  168 lines

  1. /*===========================================================================*/
  2. /*                                                                           */
  3. /* File    : TICK.C                                                          */
  4. /*                                                                           */
  5. /* Purpose : Routines pertaining to the adding and modification of tickers.  */
  6. /*                                                                           */
  7. /* History :                                                                 */
  8. /*                                                                           */
  9. /* Written by Marc Adler/Magma Systems for Microsoft Systems Journal         */
  10. /*===========================================================================*/
  11.  
  12. #include <windows.h>
  13. #include "stock.h"
  14.  
  15.  
  16. /****************************************************************************/
  17. /*                                                                          */
  18. /* Function : AddTickDlgProc()                                              */
  19. /*                                                                          */
  20. /* Purpose  : Dialog box proc for adding tickers.                           */
  21. /*                                                                          */
  22. /* Returns  :                                                               */
  23. /*                                                                          */
  24. /****************************************************************************/
  25. BOOL FAR PASCAL AddTickDlgProc(hDlg, msg, wParam, lParam)
  26.   HWND hDlg;
  27.   WORD msg;
  28.   WORD wParam;
  29.   LONG lParam;
  30. {
  31.   RECT        r;
  32.   char        buf[80];
  33.   LPSTOCKINFO lpStockInfo;
  34.   LPTICK      lpTick;
  35.   TICK        tick;
  36.   HWND        hWnd;
  37.  
  38.   static HANDLE hStockInfo;
  39.  
  40.  
  41.   switch (msg)
  42.   {
  43.     case WM_INITDIALOG:
  44.       /*
  45.          Make sure that there is a current stock information record which
  46.          we can append the ticker onto.
  47.       */
  48.       if (!hCurrStockInfo)
  49.         EndDialog(hDlg, FALSE);
  50.       return TRUE;
  51.  
  52.  
  53.     case WM_COMMAND:
  54.       switch (wParam)
  55.       {
  56.         /*
  57.           The user chose the OK button...
  58.         */
  59.         case IDOK:
  60.           /*
  61.             Get a pointer to the stock info record
  62.           */
  63.           lpStockInfo = (LPSTOCKINFO) GlobalLock(hCurrStockInfo);
  64.           if (lpStockInfo == NULL)
  65.           {
  66.             MessageBox(hDlg, "GlobalLock returned NULL", "Error", MB_OK);
  67.             goto byebye;
  68.           }
  69.  
  70.           /*
  71.             Are we entering the first ticker? If so, then allocate a
  72.             ticker array for the stock. We allocate 64 tickers initially.
  73.           */
  74.           if (lpStockInfo->hTicks == NULL)
  75.           {
  76.             lpStockInfo->StockFile.nTicks = 0;
  77.             lpStockInfo->nTicksAllocated  = 64;
  78.             lpStockInfo->hTicks = GlobalAlloc(GMEM_MOVEABLE,
  79.                                               (DWORD) sizeof(TICK) * 64);
  80.             if (lpStockInfo->hTicks == NULL)
  81.             {
  82.               MessageBox(hDlg, "Can't allocate initial ticks", "Error", MB_OK);
  83.               goto byebye;
  84.             }
  85.           }
  86.  
  87.           /*
  88.             Make sure that we do not overflow the ticker array. If there
  89.             is a chance of this, then reallocate.
  90.           */
  91.           if (lpStockInfo->StockFile.nTicks + 1 >= lpStockInfo->nTicksAllocated)
  92.           {
  93.             HANDLE h;
  94.             lpStockInfo->nTicksAllocated *= 2;
  95.             h = GlobalReAlloc(lpStockInfo->hTicks,
  96.                               (DWORD) sizeof(TICK)*lpStockInfo->nTicksAllocated,
  97.                               GMEM_MOVEABLE);
  98.             if (h == NULL)
  99.             {
  100.               MessageBox(hDlg,"Could not allocate enough memory for the tickers",
  101.                               "Error", MB_OK);
  102.               goto byebye;
  103.             }
  104.             else
  105.               lpStockInfo->hTicks = h;
  106.           }
  107.  
  108.           /*
  109.             Get a pointer to the ticker array
  110.           */
  111.           if ((lpTick = (LPTICK) GlobalLock(lpStockInfo->hTicks)) == NULL)
  112.           {
  113.             MessageBox(hDlg, "GlobalLock returned NULL", "Error", MB_OK);
  114.             GlobalUnlock(hCurrStockInfo);
  115.             goto byebye;
  116.           }
  117.  
  118.           /*
  119.             Get the price and volume and out it in the last element of the
  120.             ticker array.
  121.             KLUDGE NOTES :
  122.               a) We really should sort the records by date...
  123.               b) We ignore the date here.
  124.           */
  125.           tick.price = GetDlgItemLong(hDlg,ID_TICK_PRICE,NULL,FALSE);
  126.           tick.dwVolume = GetDlgItemLong(hDlg,ID_TICK_VOLUME,NULL,FALSE);
  127.  
  128.           /*
  129.             Copy the ticker structure
  130.           */
  131.           lpTick[lpStockInfo->StockFile.nTicks++] = tick;
  132.           lpStockInfo->dwFlags |= STATE_DIRTY;
  133.  
  134.           /*
  135.             Unlock the memory and get outta here...
  136.           */
  137.           GlobalUnlock(lpStockInfo->hTicks);
  138.           GlobalUnlock(hCurrStockInfo);
  139.           EndDialog(hDlg, TRUE);
  140.           break;
  141.  
  142.  
  143.         /*
  144.           The user chose the CANCEL button....
  145.         */
  146.         case IDCANCEL :
  147. byebye:
  148.           EndDialog(hDlg, FALSE);
  149.           break;
  150.       }
  151.       return TRUE;
  152.  
  153.     default:
  154.       return FALSE;
  155.   }
  156. }
  157.  
  158.  
  159. LONG GetDlgItemLong(HWND hDlg, WORD id, BOOL FAR *lpTranslated, BOOL bSigned)
  160. {
  161.   extern long atol();
  162.   char szBuf[64];
  163.  
  164.   GetDlgItemText(hDlg, id, (LPSTR) szBuf, sizeof(szBuf));
  165.   return atol(szBuf);
  166. }
  167.  
  168.